import { ref, reactive } from 'vue'
import { getTagsData, delTag, changeTag } from '@/network/api/tags'
import { ElMessage } from 'element-plus'
export const useTagsDataEffect = () => {
    const tagData = ref([])
    const editKey = ref(['id', 'name'])
    const pageSize = ref(5);
    const currentPage = ref(1);
    const count = ref(null)
    const initTagsData = async (params = { pageSize: pageSize.value, currentPage: currentPage.value }) => {
        const res = await getTagsData(params)
        tagData.value = res.result.data
        count.value = res.result.count
    }
    const handleSizeChange = (val) => {
        pageSize.value = val
        initTagsData({ pageSize: val, currentPage: currentPage.value })
    }
    const handleCurrentChange = (val) => {
        currentPage.value = val
        initTagsData({ pageSize: pageSize.value, currentPage: val })
    }
    initTagsData()
    return {
        tagData,
        editKey,
        pageSize,
        currentPage,
        count,
        initTagsData,
        handleCurrentChange,
        handleSizeChange,
    }
}

export const useEventEffect = (func) => {
    const editData = reactive({ id: '', name: '' })
    const dialogFlag = ref(false)
    const handleDelete = (index, row) => {
        delTag(row.id).then(res => {
            ElMessage.success(res.msg)
            func()
        })
    }
    const handleEdit = (index, row) => {
        editData.id = row.id
        editData.name = row.name
        dialogFlag.value = true
    }
    const handleClose = (text) => {
        if (text === '确定') {
            changeTag(editData).then(res => {
                if (res.status == 200) {
                    ElMessage.success('修改成功')
                    func()
                }
            })
        }
        dialogFlag.value = false
    }
    return {
        editData,
        dialogFlag,
        handleDelete,
        handleEdit,
        handleClose
    }
}